package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void insert(Employee record, Long[] ids) {
        employeeMapper.insert(record);
        if (ids!=null&&ids.length>0){
            for (Long id : ids) {
                employeeMapper.insertRelation(record.getId(),id);
            }
        }
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {

        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {

        return employeeMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Employee record, Long[] ids) {
        employeeMapper.updateByPrimaryKey(record);
        employeeMapper.deleteRelation(record.getId());
        if (ids!=null&&ids.length>0){
            for (Long id : ids) {
                employeeMapper.insertRelation(record.getId(),id);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentpage(), qo.getPageSize());

        List<Employee> Employees = employeeMapper.selectForList(qo);
        return new PageInfo<Employee>(Employees);
    }

    @Override
    public Employee login(String username, String password)  {
        if (StringUtils.hasText(username)){
            throw new LogicException("用户名不能为空");
        }
        Employee employee=employeeMapper.selectByusernameInpassword(username,password);
        if (employee==null){
            throw new LogicException("账号密码错误");
        }
        return employee;
    }

    @Override
    public void batchDelete(Long[] ids) {

        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee selectByname(String name, Long id) {

        return employeeMapper.selectByname(name,id);
    }



    @Override
    public Workbook exportXls() {
        //创建一个工作簿对象
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = wb.createSheet("员工通讯录");
        //查询员工数据
        List<Employee> employees = employeeMapper.selectAll();
        //创建行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        //每个员工一行数据

            for (int i = 0; i <employees.size() ; i++) {
                Employee employee = employees.get(i);
                row = sheet.createRow(i+1);
                row.createCell(0).setCellValue(employee.getName());
                row.createCell(1).setCellValue(employee.getEmail());
                row.createCell(2).setCellValue(employee.getAge());
            }
        return wb;
    }

    @Override
    public void improtXls(MultipartFile file) {
        try {
            //使用Poi去读取上传过来的文件
            HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
             //获取工作表
            HSSFSheet sheetAt = wb.getSheetAt(0);
            //读取行数
            //获取最后一行的行数
            int last=sheetAt.getLastRowNum();
            for (int i = last; i > 0; i--) {
                HSSFRow row = sheetAt.getRow(i);
                String name = row.getCell(0).getStringCellValue();
                String email = row.getCell(1).getStringCellValue();
                String age = row.getCell(2).getStringCellValue();
                Employee employee = new Employee();
                employee.setName(name);
                employee.setEmail(email);
                employee.setAge(Integer.valueOf(age));
                employeeMapper.insert(employee);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
